package com.bigShen.mybatis.proxy;

import com.bigShen.mybatis.session.SqlSessionShen;
import com.bigShen.mybatis.util.SqlCommandType;

/**
 * @author : lei.shen
 * @date : 2018/12/25
 */
public class MapperMethod {

    SqlCommand sqlCommand;

    public void setSqlCommand(SqlCommand sqlCommand) {
        this.sqlCommand = sqlCommand;
    }

    public Object execute(SqlSessionShen sqlSession, Object[] args) {

        Object result = null;
        String sqlId = sqlCommand.getName();
        String sql = sqlSession.getConfiguration().getSql(sqlId);
        System.out.println(String.format("[sql: %s] [params: %s]", sql, args[0]));

        switch (sqlCommand.getSqlCommandType()) {
            case INSERT:
                System.out.println("insert");
                break;
            case UPDATE:
                break;
            case DELETE:
                break;
            case SELECT:
                result = sqlSession.getExecutor().selectOne(sql, args[0]);
                break;
            default:
                throw new RuntimeException("Unknown sqlCommand Type for : " + sqlCommand.getName());
        }

        return result;
    }

    public static class SqlCommand {

        String name;

        SqlCommandType sqlCommandType;

        public SqlCommand(String name, SqlCommandType sqlCommandType) {
            this.name = name;
            this.sqlCommandType = sqlCommandType;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public SqlCommandType getSqlCommandType() {
            return sqlCommandType;
        }

        public void setSqlCommandType(SqlCommandType sqlCommandType) {
            this.sqlCommandType = sqlCommandType;
        }
    }

}

